//
//  RegisterVC.m
//  EJL
//
//  Created by adc on 15/11/4.
//  Copyright © 2015年 江苏一键联信息科技有限公司. All rights reserved.
//

#import "RegisterVC.h"
//#import "HttpManage.h"
//#import "ZhuCeDL.h"
#import "HttpsManager.h"
#import "YongHuXieYiViewController.h"
@interface RegisterVC ()

@end

@implementation RegisterVC

- (void)viewDidLoad {
    [super viewDidLoad];
   // self.title=@"注册";
    UIView*viewbg=[[UIView alloc]initWithFrame:CGRectMake(0, 20,WIDE , 44)];
    viewbg.backgroundColor=RGB_COLOR(0x00c49f);
    [self.view addSubview:viewbg];
    UIImageView*bgImage=[[UIImageView alloc]initWithFrame:CGRectMake((WIDE-134)/2,70,134, 125)];
    bgImage.image=[UIImage imageNamed:@"login"];
    [self.view addSubview:bgImage];
    UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(WIDE/2-35,7, 70,30)];
    label.text=@"用户注册";
    
    label.textAlignment=NSTextAlignmentCenter;
    label.textColor=[UIColor whiteColor];
    [viewbg addSubview:label];
    UIButton*but1=[[UIButton alloc]initWithFrame:CGRectMake(10,7,30, 30)];
    [but1 setImage:[UIImage imageNamed:@"返回箭头1"] forState:UIControlStateNormal];
    [but1 addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    [viewbg addSubview:but1];

    UIView *View1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0,30,40)];
    
    UIImageView*viewImage1=[[UIImageView alloc]initWithFrame:CGRectMake(8,10,17,20)];
    
    [viewImage1 setImage:[UIImage imageNamed:@"用户图标"]];
    [View1 addSubview:viewImage1];
    _shouji.leftView=View1;
    
    _shouji.leftViewMode=UITextFieldViewModeAlways;
    [_shouji setLeftViewMode:UITextFieldViewModeAlways];
    
    UIView *View2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0,30,40)];
    
    UIImageView*viewImage2=[[UIImageView alloc]initWithFrame:CGRectMake(8,10,17,20)];
    
    [viewImage2 setImage:[UIImage imageNamed:@"密码图标"]];
    [View2 addSubview:viewImage2];
    _mima.leftView=View2;
    
    _mima.leftViewMode=UITextFieldViewModeAlways;
    [_mima setLeftViewMode:UITextFieldViewModeAlways];
    
    UIView *View3 = [[UIView alloc] initWithFrame:CGRectMake(0, 0,30,40)];
    
    UIImageView*viewImage3=[[UIImageView alloc]initWithFrame:CGRectMake(8,10,17,20)];
    
    [viewImage3 setImage:[UIImage imageNamed:@"密码图标"]];
    [View3 addSubview:viewImage3];
    _aginemima.leftView=View3;
    
    _aginemima.leftViewMode=UITextFieldViewModeAlways;
    [_aginemima setLeftViewMode:UITextFieldViewModeAlways];
    UIView *View4 = [[UIView alloc] initWithFrame:CGRectMake(0, 0,30,40)];
    
    UIImageView*viewImage4=[[UIImageView alloc]initWithFrame:CGRectMake(8,10,17,20)];
    
    [viewImage4 setImage:[UIImage imageNamed:@"推荐人图标"]];
    [View4 addSubview:viewImage4];
    _tuijianren.leftView=View4;
    
    _tuijianren.leftViewMode=UITextFieldViewModeAlways;
    [_tuijianren setLeftViewMode:UITextFieldViewModeAlways];
//    UIImageView*view4=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0,20,25)];
//    view4.image=[UIImage imageNamed:@"推荐人图标"];
//   _tuijianren.leftView=view4;
//    
//   _tuijianren.leftViewMode=UITextFieldViewModeAlways;
    UIView *View6 = [[UIView alloc] initWithFrame:CGRectMake(0, 0,30,40)];
    
    UIImageView*viewImage6=[[UIImageView alloc]initWithFrame:CGRectMake(8,10,17,20)];
    
    [viewImage6 setImage:[UIImage imageNamed:@"手机验证码"]];
    [View6 addSubview:viewImage6];
    _yanzhengma.leftView=View6;
    
    _yanzhengma.leftViewMode=UITextFieldViewModeAlways;
    [_yanzhengma setLeftViewMode:UITextFieldViewModeAlways];

    UIView *View5 = [[UIView alloc] initWithFrame:CGRectMake(0, 0,110,40)];
    UIButton*but=[[UIButton alloc]initWithFrame:CGRectMake(0,5,100,30)];
    //[but.imageView=view5];
    [but setTitle:@"获取验证码" forState:UIControlStateNormal];
    [but setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [but setBackgroundColor:RGB_COLOR(0x00c49f)];
    but.layer.cornerRadius=5;
    //[but setImage:[UIImage imageNamed:@"获取验证码"] forState:UIControlStateNormal];
    but.titleLabel.font=[UIFont boldSystemFontOfSize:15.0f];
    [but addTarget:self action:@selector(butClick) forControlEvents:UIControlEventTouchUpInside];
    [View5 addSubview:but];
    _yanzhengma.rightView=View5;
    
    _yanzhengma.rightViewMode=UITextFieldViewModeAlways;
    [_yanzhengma setLeftViewMode:UITextFieldViewModeAlways];    _shouji.delegate=self;
    _mima.delegate=self;
    _aginemima.delegate=self;
    _tuijianren.delegate=self;
    _yanzhengma.delegate=self;
        // Do any additional setup after loading the view from its nib.
}
-(void)backClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)butClick
{
     
    JGProgressHUD *HUD = [[JGProgressHUD alloc] initWithStyle:JGProgressHUDStyleDark];
    
    
    HUD.useProgressIndicatorView = YES;
 
    [HUD showInView:self.navigationController.view];

    [HttpsManager requestGetShouJiYanZhengMa:_shouji.text compeletion:^(int errcode, NSString *errmsg)
    {
        NSString*str=[NSString stringWithFormat:@"%d",errcode];
        if ([str isEqualToString:@"0"])
        {
           
         
            HUD.textLabel.text = errmsg;
            [HUD dismissAfterDelay:1.f];
            return ;
        }
        
        HUD.textLabel.text = errmsg;
        [HUD dismissAfterDelay:1.f];

    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//开始编辑输入框的时候，软键盘出现，执行此事件
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    CGRect frame = textField.frame;
    int offset = frame.origin.y + 32 - (self.view.frame.size.height - 245.0);//键盘高度216
    
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    
    //将视图的Y坐标向上移动offset个单位，以使下面腾出地方用于软键盘的显示
    if(offset > 0)
        self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
    
    [UIView commitAnimations];
}

//当用户按下return键或者按回车键，keyboard消失
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

//输入框编辑完成以后，将视图恢复到原始状态
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    self.view.frame =CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)zhuce:(UIButton *)sender

{

 
    JGProgressHUD *HUD = [[JGProgressHUD alloc] initWithStyle:JGProgressHUDStyleDark];
    
    
    HUD.useProgressIndicatorView = YES;
 
    [HUD showInView:self.navigationController.view];
    if ([_mima.text isEqualToString:@""]) {
        HUD.textLabel.text = @"密码不能为空！";
        [HUD dismissAfterDelay:0.3];
    }else{
    if (_mima.text.length<6||_mima.text.length>12) {
        
        
                HUD.textLabel.text = @"请输入长度6到12位的密码!";
                [HUD dismissAfterDelay:0.3];
        
    }else{
        
        if(_mima.text !=_aginemima.text) {
            HUD.textLabel.text = @"密码不符,请检查后重新输入！";
            [HUD dismissAfterDelay:0.3];
        }else{
 
        ZhuCeDL*zhuCe=[[ZhuCeDL alloc]init];
        
        zhuCe.password=[MD5 md532BitLower:_mima.text ];
        zhuCe.phone=_shouji.text;
        zhuCe.yanZhengMa=_yanzhengma.text;
        zhuCe.inviter=_tuijianren.text;
        [HttpsManager registerRequestWithPhoneNumber:zhuCe completion:^(int errcode, NSString *errmsg)
         {
             NSString*str=[NSString stringWithFormat:@"%d",errcode];
             if ([str isEqualToString:@"0"])
             {
                 SHOW_ALERT(errmsg);
                 HUD.textLabel.text = errmsg;
                 [HUD dismiss];
                 [self.navigationController popViewControllerAnimated:YES];
                 return ;
             }
             
             HUD.textLabel.text = errmsg;
             [HUD dismissAfterDelay:1.0f];
         }];

    }
    }
    }
}

- (IBAction)xieYiBut:(UIButton *)sender
{
    
    YongHuXieYiViewController*vc=[[YongHuXieYiViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    
    
}
@end
